﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace DataExtractionLanguage
{
    [Serializable]
    public class ExpressionTerm
    {
        public Term Term { get; set; }
        public TokenTypeEnum TermType { get; set; }
        public object Value { get; set; }

        public ExpressionTerm Next { get; set; }

        public override string ToString()
        {
            string retVal = string.Empty;
            ExpressionTerm expAux = this;
            while (expAux != null)
            {
                if (expAux.TermType == TokenTypeEnum.Number)
                    retVal += string.Format("{0}", expAux.Value);
                else if (expAux.TermType == TokenTypeEnum.String)
                    retVal += string.Format("'{0}'", expAux.Value);
                else if (expAux.TermType == TokenTypeEnum.Term)
                    retVal += expAux.Term.ToString();
                else
                    retVal += string.Format("{0}", expAux.TermType);

                if (expAux.Next != null)
                    retVal += "  ";

                expAux = expAux.Next;
            }

            return retVal;
        }

        public ExpressionTerm Clone()
        {
            MemoryStream stream = new MemoryStream();
            BinaryFormatter s = new BinaryFormatter();
            s.Serialize(stream, this);
            stream.Seek(0, SeekOrigin.Begin);
            return (ExpressionTerm)s.Deserialize(stream);
        }
    }
}
